package ${classPackageNameMap[table.className+'JsfServiceImpl']};

import com.jd.jxt.jxpp.common.exception.BusinessException;
import ${classTypeNameMap[table.className+'VO']};
import ${classTypeNameMap[table.className+'JsfService']};
import ${classTypeNameMap[table.className]};
import ${classTypeNameMap[table.className+'Example']};
import ${classTypeNameMap[table.className+'Service']};
import com.jd.jxt.jxpp.wechat.robot.util.ParamUtil;
import com.jd.xtl.common.spi.response.APIResult;
import com.jd.xtl.common.spi.response.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ${author!}
 * @since ${timeNow!}
 * ${table.remarks!}
 */
@Slf4j
@Service
public class ${table.className}JsfServiceImpl implements ${table.className}JsfService {
    @Resource
    private ${table.className}Service ${table.classNameFirstLower}Service;
    @Override
    public APIResult<${table.className+'VO'}> selectOne(${table.className+'VO'} request) {
        log.info("${table.className}JsfServiceImpl.selectOne,根据条件查询对象,${table.className+'VO'}:{}",request);
        try {
            List<${table.className+'VO'}> list = ${table.classNameFirstLower}Service.selectList(request);
            return APIResult.success(CollectionUtils.isEmpty(list)?null:list.get(0));
        }catch (BusinessException e){
            return APIResult.result(NumberUtils.toInt(e.getCode()),e.getMessage(),null);
        }
    }
    @Override
    public APIResult${"\l"}List<${table.className+'VO'}>> selectList(${table.className+'VO'} request) {
        log.info("${table.className}JsfServiceImpl.selectList,查询列表,${table.className}:{}",request);
        try {
            List<${table.className+'VO'}> res = ${table.classNameFirstLower}Service.selectList(request);
            return APIResult.success(res);
        }catch (BusinessException e){
            return APIResult.result(NumberUtils.toInt(e.getCode()),e.getMessage(),null);
        }
    }
    @Override
    public APIResult${"\l"}PageResult<${table.className+'VO'}>> selectPage(${table.className+'VO'} request){
        log.info("${table.className}JsfServiceImpl.selectPage,查询列表,${table.className+'VO'}:{}",request);
        try {
            PageResult<${table.className+'VO'}> res = ${table.classNameFirstLower}Service.selectPage(request);
            return APIResult.success(res);
        }catch (BusinessException e){
            return APIResult.result(NumberUtils.toInt(e.getCode()),e.getMessage(),null);
        }
    }
    @Override
    public APIResult${"\l"}Integer> deleteById(${table.className+'VO'} request){
        log.info("${table.className}JsfServiceImpl.deleteById,根据id删除,request:{}",request);
        try {
            ParamUtil.check(request).nullThrow("请求参数").thenCheck(request.getId()).nullThrow("id");
            ${table.className}Example example = new ${table.className}Example();
            example.createCriteria().andIdEqualTo(request.getId());
            Integer res = ${table.classNameFirstLower}Service.delete(example);
            return APIResult.success(res);
        }catch (BusinessException e){
            return APIResult.result(NumberUtils.toInt(e.getCode()),e.getMessage(),null);
        }
    }
    @Override
    public APIResult${"\l"}Integer> insertBatch(List<${table.className+'VO'}> requests) {
        log.info("${table.className}ServiceImpl.insertBatch,批量新增,${table.className+'VO'}.size():{}",requests.size());
        try {
            Integer num = ${table.classNameFirstLower}Service.insertBatch(requests);
            return APIResult.success(num);
        }catch (BusinessException e){
            return APIResult.result(NumberUtils.toInt(e.getCode()),e.getMessage(),null);
        }
    }
    @Override
    public APIResult${"\l"}Long> save(${table.className+'VO'} request) {
        log.info("${table.className}JsfServiceImpl.save,保存,request:{}",request);
        try {
            ${table.className} entity = new ${table.className}();
            BeanUtils.copyProperties(request,entity);
            ${table.classNameFirstLower}Service.save(entity);
            return APIResult.success(entity.getId());
        }catch (BusinessException e){
            return APIResult.result(NumberUtils.toInt(e.getCode()),e.getMessage(),null);
        }
    }

}